<template>
	<view class="mainpadding">
		<view class="flexleft xiahuaxian">
			<view class="margin_right2">
				<u-icon name="bell-fill" color="#919399" size="20"></u-icon>
			</view>
			<view class="xiaohui ershil">
				你的举报将在12小时内受理，若举报成功会第一时间告 知处理结果，请尽量提交完整的举报描述及材料。
			</view>
		</view>
		<view class="flexbetween margin_top5">
			<view class="sanshier fonweight xiaohui">举报理由</view>
			<view class="flexright">
				<view class="sanshier fonweight xiaohei">{{jbdata.name}}</view>
				<u-icon name="arrow-right" color="#333333" size="18"></u-icon>
			</view>
		</view>
		<view class="sanshier fonweight xiaohui margin_top5">举报描述(选填)</view>
		<view class="margin_top">
			<u--textarea v-model="content" placeholder="请输入内容" maxlength="200" height="130" count></u--textarea>
		</view>
		<view class="flexleft flex_wrap">
			<view class="flexcenter tpbox " v-for="(item,index) in images_text" :key="index">
				<view class="tjtp">
					<image class="" :src="item" mode=""></image>
					<view class="deleteimg" @click="deleteimg(index,images,images_text)"></view>
				</view>
			</view>
			<view class="flexcenter tpbox " v-if="images.length<6" @click="uploadImg(6,images,images_text)">
				<image class="tjtp" src="../static/image/system/tjtp.png" mode=""></image>
			</view>
		</view>
		<view class="strongtext xiaohui margin_top">请上传举报凭证，最多上传6张</view>
		<view class="gudingdb ffffff mainpadding">
			<view class="bigbtn" @click="submit">提交</view>
		</view>
		<yk-authpup ref="authpup" type="top" @changeAuth="changeAuth" :permissionID="permissionID"></yk-authpup>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import ykAuthpup from "@/components/yk-authpup/yk-authpup";
	export default {
		components: {
			ykAuthpup
		},
		data() {
			return {
				jbdata: {},
				images: [],
				images_text: [],
				content: "",
				permissionID: '',
				sourceType: ['camera'],
				upnum: 0
			}
		},
		onLoad(options) {
			this.jbdata = JSON.parse(decodeURIComponent(options.msg))
		},
		methods: {
			submit() {
				// if (this.images.length == 0) {
				// 	httpRequest.toast("请上传举报凭证")
				// 	return false
				// }
				uni.showLoading({
					mask: true,
					title: "请稍后"
				})
				httpRequest.request('/api/user/addReport', 'GET', {
					content: this.content,
					images: this.images.join(),
					report_type_id: this.jbdata.id,
				}).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack({
								delta: 2
							})
						}, 1000)
					}
				})
			},
			deleteimg(index, upImgs, upImgs_text) {
				upImgs.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			openAuth(permissionID) {
				this.permissionID = permissionID; //这个是对应的权限 ACCESS_FINE_LOCATION 位置权限 / WRITE_EXTERNAL_STORAGE 存储空间/照片权限 / CAMERA相机权限 / CALL_PHONE 拨打电话
				setTimeout(() => {
					this.$refs['authpup'].open();
				}, 500)
			},
			uploadImg(value, upImgs, upImgs_text) {
				let _this = this;
				let num = 1
				if (value > 2) {
					num = Number(value) - upImgs.length;
					this.upnum = num
					if (num <= 0) {
						return false;
					}
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						if (uni.getSystemInfoSync().brand.toLowerCase() === 'huawei') {
							if (res.tapIndex == 0) {
								_this.openAuth('CAMERA');
								_this.sourceType = ['camera'];
							} else {
								_this.openAuth('WRITE_EXTERNAL_STORAGE');
								_this.sourceType = ['album'];
							}
			
						} else {
							if (res.tapIndex == 0) {
			
								_this.sourceType = ['camera'];
							} else {
			
								_this.sourceType = ['album'];
							}
							_this.changeAuth();
						}
			
					},
					fail: function(res) {}
				});
			},
			changeAuth() {
				let _this = this;
				// 选择相机走0
				if (this.sourceType == "camera") {
					httpRequest.chooseImage(this.upnum, ['camera']).then(res => {
						for (var i = 0; i < res.length; i++) {
							_this.uploadImageService(res[i])
						}
					}).catch(err => {});
				}
				// 选择相册走1
				if (this.sourceType == "album") {
					httpRequest.chooseImage(this.upnum, ['album']).then(res => {
						for (var i = 0; i < res.length; i++) {
							_this.uploadImageService(res[i])
						}
					}).catch(err => {});
				}

			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/addons/cos/index/upload1', url).then(res => {
					let image_item = res.fullurl;
					_this.images.push(res.url)
					_this.images_text.push(image_item)
				}).catch(err => {});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.tpbox {
		width: 33.33%;
		margin-top: 30rpx;
	}

	.tjtp {
		width: 209rpx;
		height: 209rpx;
		position: relative;

		image {
			border-radius: 10rpx;
			width: 100%;
			height: 100%;
		}
	}

	.jbtp {
		width: 150rpx;
		height: 150rpx;
		border-radius: 20rpx;

		image {
			border-radius: 20rpx;
			width: 100%;
			height: 100%;
		}
	}

	.shangbox {
		width: 150rpx;
		height: 150rpx;
		background: #F9F9F9;
		border-radius: 20rpx;
	}
</style>